type name = string;;
type age = int;;
type adress = string;;
type telephone = int;;
type contact = (name * age * adress * telephone);;
type agenda = contact list;;

let rec exist x liste = match liste with
    [] -> false
  | e::list -> x = e || (exist x list);;

let rec add_list x l = match l with
    [] -> x::[]
  | e::reste when e = x -> l
  | e::reste when e < x -> e::add_list x reste
  | e::reste -> e::x::reste;;

let rec remove_list x l = match l with
    [] -> invalid_arg "pas d'element a supprime"
  | e::reste when e = x -> reste
  | e::reste -> e::(remove_list x reste);;

let rec map f l = match l with
    [] -> []
  | e::list -> (f e)::(map f list);;

let rec iter f l = match l with
    [] -> ()
  | e::reste -> begin f e;
               iter f reste end;;

iter (print_int) [1;3;5;7;11];;

let rec for_all  p list = match list with
    [] -> true
  | e::reste -> p e&&(for_all p reste);;

for_all (function x -> x = 0) [0;0;0;0;0];;

let upper_case c =
  if int_of_char c >= 97 && int_of_char c <= 122 then
    char_of_int((int_of_char c) - 32)
  else
    c ;;

upper_case 'c';;

let lower_case c = 
  if int_of_char c >= 65 && int_of_char c <= 90 then
    char_of_int((int_of_char c) + 32)
  else
    c ;;

lower_case 'C';;

let string_of_char c = String.make 1 c;;

let string_apply f str =
  let rec calcul i = match i with
      0 -> string_of_char (f str.[0])
    | i -> (calcul (i-1))^(string_of_char ( f str.[i]))
  in
    calcul ((String.length str) - 1);;

let string_uppercase str = string_apply upper_case str;; 

string_uppercase "Hello";;

let string_lowercase str = string_apply lower_case str;;

string_lowercase "HElLO";;

let convert_first_maj str = 
  string_apply (function x -> if x = str.[0] then
                         upper_case str.[0]
                       else
                         lower_case x) str;;

convert_first_maj "hello";;

let create_contact contact = match contact with
    (name,age,adress,telephone) -> ((convert_first_maj name),age,adress,telephone);;   

let rec manage_contact f contact agenda = f contact agenda;;
